package com.wrupple.muba.catalogs.client.cms;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.Image;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.wrupple.muba.catalogs.client.activity.impl.AbstractCatalogActivity;
import com.wrupple.muba.catalogs.client.activity.process.CatalogSelectionProcess;
import com.wrupple.muba.catalogs.client.activity.process.impl.BrowsingActivityProcess;
import com.wrupple.muba.catalogs.client.activity.process.state.BrowsePlaceInterpret;
import com.wrupple.muba.catalogs.client.activity.process.state.InteractionExitState;
import com.wrupple.muba.catalogs.client.cms.service.CatalogManagementSystem;
import com.wrupple.muba.catalogs.client.module.services.presentation.CatalogPlaceInterpret;
import com.wrupple.muba.catalogs.client.view.CatalogExportView;
import com.wrupple.muba.catalogs.client.widgets.impl.CatalogTheme;
import com.wrupple.muba.catalogs.client.widgets.impl.CatalogUserInterfaceMessages;
import com.wrupple.muba.catalogs.domain.CatalogCreatePlace;
import com.wrupple.muba.catalogs.domain.CatalogEntry;
import com.wrupple.muba.catalogs.domain.CatalogEntryImportPlace;
import com.wrupple.muba.catalogs.domain.JsCatalogKey;
import com.wrupple.muba.common.client.activity.ActivityProcess;
import com.wrupple.muba.common.client.application.DataCallback;
import com.wrupple.muba.common.domain.DesktopPlace;
import com.wrupple.muba.common.shared.Process;
import com.wrupple.muba.common.shared.SequentialProcess;
import com.wrupple.muba.common.shared.State.DesktopManager;
import com.wrupple.muba.common.shared.State.ProcessManager;
import com.wrupple.muba.common.shared.State.StorageManager;
import com.wrupple.muba.common.shared.StateTransition;

public class ContentManagementActivity extends AbstractCatalogActivity<ProcessManager> {
	public class StartDelete implements ClickHandler {
		CatalogSelectionProcess<JsCatalogKey> process ;
		
		public StartDelete(CatalogSelectionProcess<JsCatalogKey> process) {
			super();
			this.process = process;
		}

		@Override
		public void onClick(ClickEvent event) {
			StateTransition callback = new DeleteEntries();
			process.startSelection(callback);
		}

	}

	class DeleteEntries extends DataCallback<List<CatalogEntry>> {

		@Override
		public void execute() {
			List<String> ids = new ArrayList<String>(result.size());
			StateTransition<CatalogEntry> callback;
			for (CatalogEntry e : result) {
				ids.add(e.getIdAsString());
				callback = new DeleteCallback(e);
				sm.delete(catalog, e.getIdAsString(), callback);
			}
			
			
		}

	}
	
	class DeleteCallback extends DataCallback<CatalogEntry>{

		private CatalogEntry entryToDelete;

		public DeleteCallback(CatalogEntry e) {
			this.entryToDelete=e;
		}

		@Override
		public void execute() {
			boolean success= result!=null && result.getIdAsString().equals(entryToDelete.getIdAsString());
			if(success){
				
			}else{
				//retry
			}
		}
		
	}
	public class StartCreate implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			dm.goTo(new CatalogCreatePlace(catalog));
		}

	}

	class ExportCallback extends DataCallback<Void> {

		@Override
		public void execute() {
			// do nothing when done exporting?
		}

	}

	class StartExport implements ClickHandler {
		CatalogSelectionProcess<JsCatalogKey> process ;
		
		public StartExport(CatalogSelectionProcess<JsCatalogKey> process) {
			super();
			this.process = process;
		}



		@Override
		public void onClick(ClickEvent event) {
			List list = process.getBrowseView()
					.getVisibleEntries();
			List<CatalogEntry> input = list;

			CatalogExportView view = exportViewProvider.get();

			Process<List<CatalogEntry>, Void> process = SequentialProcess
					.wrap(view);

			webapp.processSwitch(process, constants.exportEntries(), input,
					new ExportCallback(), eventBus);
		}

	}

	

	class StartImport implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			dm.goTo(new CatalogEntryImportPlace(catalog));
		}

	}
	private Provider<CatalogExportView> exportViewProvider;
	protected CatalogManagementSystem cms;
	protected BrowsePlaceInterpret read;
	protected InteractionExitState exit;
	protected CatalogTheme theme;
	protected CatalogUserInterfaceMessages constants;
	protected StorageManager sm;
	
	@Inject
	public ContentManagementActivity(ProcessManager pm,CatalogManagementSystem cms,StorageManager sm,
			CatalogPlaceInterpret placeInterpret,
			DesktopManager dm,BrowsePlaceInterpret read, InteractionExitState exit ,CatalogTheme theme,CatalogUserInterfaceMessages constants) {
		super(pm, placeInterpret, dm);
		this.sm=sm;
		this.cms=cms;
		this.read=read;
		this.exit=exit;
		this.theme=theme;
		this.constants=constants;
		read.setPlaceInterpret(placeInterpret);
	}
	
	@Inject
	public void setExportViewProvider(Provider<CatalogExportView> exportViewProvider) {
		this.exportViewProvider = exportViewProvider;
	}


	@Override
	public void getActivityProcess(DesktopPlace item,
			DataCallback<ActivityProcess> callback) {
		CatalogSelectionProcess<JsCatalogKey> wrappedProcess = cms.getContentManager(catalog).getSelectionProcess(false);
		
		Image export = getExportButton(wrappedProcess);
		Image imnport = getImportButton();
		Image create = getCreateButtonDescriptor(theme.create(),
				new StartCreate(), constants);
		Image delete = getDeleteButtonDescriptor(theme.delete(),
				new StartDelete(wrappedProcess), constants);
		wrappedProcess.setAction(Arrays
				.asList(create, delete, imnport, export));
		
		
		ActivityProcess result = new BrowsingActivityProcess(read,wrappedProcess,exit);
		callback.setResultAndFinish(result );
	}
	
	public Image getExportButton(CatalogSelectionProcess<JsCatalogKey> wrappedProcess ) {
		String help = constants.exportEntries();
		Image export = new Image(theme.export());
		export.addClickHandler(new StartExport(wrappedProcess));
		export.setAltText(help);
		export.setTitle(help);
		return export;
	}

	public Image getImportButton() {
		String help = constants.importEntries();
		Image in = new Image(theme.imnport());
		in.setTitle(help);
		in.setAltText(help);
		in.addClickHandler(new StartImport());
		return in;
	}
	

	public Image getCreateButtonDescriptor(ImageResource imageResource,
			ClickHandler handler, CatalogUserInterfaceMessages constants) {
		String create = constants.create();
		String createTitle = constants.createHelp();
		Image button = new Image(imageResource);
		button.addClickHandler(handler);
		button.setAltText(create);
		button.setTitle(createTitle);
		return button;
	}

	public Image getDeleteButtonDescriptor(ImageResource resource,
			ClickHandler handler, CatalogUserInterfaceMessages constants) {
		String name = constants.delete();
		String help = constants.deleteHelp();
		Image delete = new Image(resource);
		delete.addClickHandler(handler);
		delete.setAltText(name);
		delete.setTitle(help);
		return delete;
	}
}
